package javaDemo03;

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class SuperMarket {
    private static class Goods {
        public String id;
        public String name;
        public String unit;
        public double price;

        public Goods(String id, String name, String unit, double price) {
            this.id = id;
            this.name = name;
            this.unit = unit;
            this.price = price;
        }

        @Override
        public String toString() {
            return id + "\t" + name + "\t" + unit + "\t" + price;
        }
    }
    private static final class BuyedGoods extends Goods {
        public int sum;

        public BuyedGoods(Goods g, int sum) {
            super(g.id, g.name, g.unit, g.price);
            this.sum = sum;
        }
    }
    private final ArrayList<Goods> data = new ArrayList<>();
    private final ArrayList<BuyedGoods> buyed = new ArrayList<>();
    private void welcome() {
        System.out.println("\t\t欢迎进入超市");
    }
    private void goodbye() {
        System.out.println("感谢使用！");
    }
    private void printMenu() {
        System.out.println("请输入你要进行的操作：");
        System.out.println("1.购买商品\t2.结算并打印小票\t3.退出系统");
    }
    private void printGoods() {
        data.forEach(System.out::println);
    }
    private int findGoods(String id) {
        for (int i = 0; i < data.size(); ++i) {
            if (data.get(i).id.equals(id)) {
                return i;
            }
        }
        return -1;
    }
    private int findBoughtGoods(String id) {
        for (int i = 0; i < buyed.size(); ++i) {
            if (buyed.get(i).id.equals(id)) {
                return i;
            }
        }
        return -1;
    }
    private void settlement() {

    }
    private void buy() {
        printGoods();
        Scanner sc = new Scanner(new BufferedInputStream(System.in));

        while (true) {
            String[] item = sc.nextLine().split("-");
            if (item.length == 1 && item[0].equals("end")) {
                System.out.println("退出购买");
                return;
            }

            if (item.length == 2) {
                // 购买数量
                int buySum = Integer.parseInt(item[1]);
                // 查找商品
                int goodsIndex = findGoods(item[0]);
                // 没找到
                if (goodsIndex == -1) {
                    System.out.println("没有指定的商品");
                    continue;
                }

                // 看看是不是第一次购买了
                int boughtIndex = findBoughtGoods(item[0]);
                // 不是第一次买了
                if (boughtIndex != -1) buyed.get(boughtIndex).sum += buySum;
                // 是第一次
                else buyed.add(new BuyedGoods(data.get(goodsIndex), buySum));
                continue;
            }

            System.out.println("输入格式不对。");
        }
    }
    public void addGoods(String id, String name, String unit, double price) {
        data.add( new Goods(id, name, unit, price) );
    }
    public void run() {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));

        welcome();
        boolean loop = true;
        while (loop) {
            printMenu();

            switch (sc.nextInt()) {
                case 1:
                    buy();
                    break;
                case 2:
                    settlement();
                    break;
                case 3:
                    loop = false;
                    break;
            }
        }
        goodbye();
    }

    public static void main(String[] args) {
        SuperMarket sm = new SuperMarket();
        sm.addGoods("001", "少林核桃", "斤", 15.6);
        sm.addGoods("002", "奶油饼干", "袋", 7.6);
        sm.addGoods("003", "移动硬盘", "个", 437);
        sm.addGoods("004", "方便面", "包", 5.6);
        sm.run();
    }
}
